package com.ping.demo;

import org.apache.commons.httpclient.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.ping.common.Md5Encrypt;
import com.ping.common.MyHttpClient;
import com.ping.controller.BaseController;
import com.ping.model.Ping;

public class HttpRequestSimulate {
	public static final String addr = "http://localhost/Ping";
	
	public static void main(String[] arugs){
		MyHttpClient httpClient = new MyHttpClient() {
			@Override
			public Object loadData(String json) {
				// TODO Auto-generated method stub
				System.out.println(json);
				return null;
			}
		};
		
		HttpRequestSimulate simulate = new HttpRequestSimulate();
		simulate.ping_add(httpClient);
	}
	
	public void store_stores(MyHttpClient httpClient){
		String url=addr+"/store/stores";
		
		JSONObject request = new JSONObject();
		try {
			request.put("token", "token");
			
			JSONObject object = new JSONObject();
			object.put("market", "6959c4b4-909e-11e3-91f1-10604b79e2de");
			
			request.put(BaseController.REQUEST, object);
			
			System.out.println(request.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NameValuePair pair = new NameValuePair("query.statement", request.toString());
		NameValuePair[] params = {pair};
		System.out.println(httpClient.post(url, params));
	}
	
	public void market_markets(MyHttpClient httpClient){
		String url=addr+"/market/markets";
		
		JSONObject request = new JSONObject();
		try {
			request.put("token", "token");
			System.out.println(request.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NameValuePair pair = new NameValuePair("query.statement", request.toString());
		NameValuePair[] params = {pair};
		System.out.println(httpClient.post(url, params));
	}
	
	public void user_login(MyHttpClient httpClient){
		String url=addr+"/user/login";
		
		JSONObject request = new JSONObject();
		try {
			request.put("token", "token");
			
			JSONObject object = new JSONObject();
			object.put("phone", "18988836325");
			object.put("pwd", "6450975E604C40AD2C844664AB8AAC99");
			
			request.put(BaseController.REQUEST, object);
			
			System.out.println(request.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NameValuePair pair = new NameValuePair("query.statement", request.toString());
		NameValuePair[] params = {pair};
		System.out.println(httpClient.post(url, params));
	}
		
	public void user_add(MyHttpClient httpClient){
		String url=addr+"/user/add";
		
		JSONObject request = new JSONObject();
		try {
			JSONObject object = new JSONObject();
			object.put("name", "李祖坚");
			object.put("phone", "18988836325");
//			object.put("email", "18988836325@189.cn");
			object.put("pwd", new Md5Encrypt().getMD5ofStr("lizujian"));
			
			request.put(BaseController.REQUEST, object);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NameValuePair pair = new NameValuePair("query.statement", request.toString());
		NameValuePair[] params = {pair};
		System.out.println(httpClient.post(url, params));
	}
	
	public void ping_add(MyHttpClient httpClient){
		String url=addr+"/pin/add";
		
		JSONObject request = new JSONObject();
		try {
			request.put("token", "token");
			
			JSONObject object = new JSONObject();
			object.put(Ping.MARKET, "6959c4b4-909e-11e3-91f1-10604b79e2de");
			object.put(Ping.CAT, 0);
			object.put(Ping.HAVE, 500);
			object.put(Ping.TARGET, 1000);
			object.put(Ping.PUBLISHER, "dbd98b7f-9099-11e3-84d8-10604b79e2de");
			object.put(Ping.STORE, "6096040a-96e8-11e3-9b5a-003067da8934");
			
			request.put(BaseController.REQUEST, object);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NameValuePair pair = new NameValuePair("query.statement", request.toString());
		NameValuePair[] params = {pair};
		System.out.println(httpClient.post(url, params));
	}
	
	public void ping_info(MyHttpClient httpClient){
		String url=addr+"/pin/info";
		
		JSONObject request = new JSONObject();
		try {
			request.put("token", "token");
			
			JSONObject object = new JSONObject();
			object.put(Ping.UUID, "5fda7ba0-9787-11e3-b578-10604b79e2de");	
			
			request.put(BaseController.REQUEST, object);
			
			System.out.println(request.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NameValuePair pair = new NameValuePair("query.statement", request.toString());
		NameValuePair[] params = {pair};
		System.out.println(httpClient.post(url, params));
	}
}
